// ==UserScript==
// @name			Fed Chat
// @namespace		marnick.leau@skynet.be
// @description		Adds Federation chatroom to the Chat page.
// @include			http*://chat.pardus.at/chat.php*
// ==/UserScript==

try {
var url = "http://webxro.host22.com/";
var name = "Fed<br>Chat";

var chats = document.getElementsByClassName('tabcontent');
var tr = chats[0].parentNode;
var table = tr.parentNode.parentNode;
var chatframe = document.getElementById('ChatFrame');

unsafeWindow.resetwebxro = function() {
	localStorage.setItem("webxro","false");
	GM_log ("Setting to False");
}

for (var i = 0;i < chats.length;i++) {
	if (chats[i].getAttribute('onmousedown') !== null) {
		chats[i].setAttribute('onmousedown', "resetwebxro();"+chats[i].getAttribute('onmousedown'));
	}
	
	if (chats[i].getAttribute('onmouseout') === null) {
		var chatact = chats[i];
		if (i === 0) {
			chatinact = chats[1];
		} else {
			chatinact = chats[0];
		}
	}
}

var xroroom = chatinact.cloneNode(true);
xroroom.id = "webxro";
xroroom.setAttribute('onmousedown',"webxro();");
xroroom.removeAttribute ('onclick');
xroroom.innerHTML = name;
tr.appendChild(xroroom);
table.width = 96*(tr.getElementsByClassName('tabcontent').length);

unsafeWindow.webxro = function() {
	chatframe.src = url;
	var label = document.getElementsByTagName('h1')[0];
	label.parentNode.removeChild(label);
	
	for (var i = 0; i < chats.length - 1;i++) {
		if (chats[i].getAttribute('onmouseout') === null) {
			var chatact = chats[i];
			if (i === 0) {
				chatinact = chats[1];
			} else {
				chatinact = chats[0];
				var active = i;
			}
			break;
		}
	}

	xroroom.setAttribute('onmouseout',chatact.getAttribute('onmouseout'));
	xroroom.removeAttribute('onmouseover');
	xroroom.setAttribute('style',chatact.getAttribute('style'));
	
	chatact.setAttribute('onmouseout',chatinact.getAttribute('onmouseout'));
	chatact.setAttribute('onmouseover',chatinact.getAttribute('onmouseover'));
	chatact.setAttribute('style',chatinact.getAttribute('style'));
	chatact.setAttribute('onmousedown',"resetwebxro();document.location.href = \"/chat.php?channel=" + ["general","rpg","trade","ally"][active] + "\";");
	
	var rubbish = chatframe.nextSibling;
	while (rubbish.nodeName !== "DIV") {
		rubbish = rubbish.nextSibling;
	}
	rubbish.parentNode.removeChild(rubbish.nextSibling.nextSibling);
	rubbish.parentNode.removeChild(rubbish);
	chatframe.parentNode.removeChild(chatframe.nextSibling);
	chatframe.parentNode.removeChild(chatframe.nextSibling);
	
	localStorage.setItem("webxro","true");
	unsafeWindow.WresizeChat ();
}

function WresizeChat () {
	if (localStorage.getItem("webxro") != "true") return;

	var height = 0;
	if (typeof(window.innerHeight) == 'number') {
	    height = window.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
	    height = document.documentElement.clientHeight;
	} else if (document.body && document.body.clientHeight) {
	    height = document.body.clientHeight;
	}

	if (height != 0) {
	    document.getElementById('ChatFrame').height = height - 100;
	}
}
unsafeWindow.WresizeChat = WresizeChat;

if (localStorage.getItem("webxro") == "true") {
	unsafeWindow.webxro();
	unsafeWindow.onload = unsafeWindow.WresizeChat ();
}

} catch(scripterror) {
	GM_log(scripterror);
}